package com.example.litepal;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.litepal.sql.Book;
import com.example.litepal.sql.MyOpenHelper;
import com.example.litepal.util.BaseActivity;
import com.example.litepal.util.L;

import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    MyOpenHelper openHelper;
   List<Book> books;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.create_database);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //  用LitePal创建数据库和表
                Connector.getDatabase();
                Toast.makeText(MainActivity.this, "表创建成功了", Toast.LENGTH_LONG).show();
            }
        });

        Button btn2 = (Button) findViewById(R.id.add_data);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                for (int i = 0; i < 50; i++) {
                    Book book = new Book();
                    book.setName("三国演义" );
                    book.setAuthor("罗贯中" );
                    book.setPages(865);
                    book.setPrice(38.2);
                    book.setPress("中信出版社");
                    book.save();
//                }

                Toast.makeText(MainActivity.this, "插入数据成功", Toast.LENGTH_LONG).show();
            }
        });

        Button btn3 = (Button) findViewById(R.id.update);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Book book = new Book();
                book.setPrice(10.2);
                book.setAuthor("三毛");
                book.updateAll("name=? and author=?","三国演义","罗贯中");

                Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_LONG).show();
            }
        });

        Button del = (Button) findViewById(R.id.delete);
        del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataSupport.deleteAll(Book.class,"price > ?","15");

                Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_LONG).show();
            }
        });

        Button select = (Button) findViewById(R.id.select);
        select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 查询book中的所有的数据

                books = DataSupport.findAll(Book.class);
                android.widget.ListView lv = (android.widget.ListView) findViewById(R.id.lv);
                lv.setAdapter(new Myadapter());

//                Toast.makeText(ListView.this,"查找数据"+books.toString(),Toast.LENGTH_LONG).show();
            }
        });
    }

    class Myadapter extends BaseAdapter {

        // 获取模型层有多少条数据
        @Override
        public int getCount() {
            return books.size();
        }

        //convertView 系统缓存的条目
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            L.d(position + "");
            Book book = books.get(position);
            /*TextView textView = new TextView(ListView.this);
            textView.setText(book.toString());
            textView.setTextSize(16);*/

            View inflate = null;
            if (convertView == null) {
                // 把布局文件填充成view对象
                inflate = View.inflate(MainActivity.this, R.layout.item_listview, null);
            } else {
                // 把缓存放入View
                inflate = convertView;
            }


            TextView tv_name = (TextView) inflate.findViewById(R.id.item_name);
            TextView tv_author = (TextView) inflate.findViewById(R.id.item_author);
            TextView tv_price = (TextView) inflate.findViewById(R.id.item_price);
            TextView tv_pages = (TextView) inflate.findViewById(R.id.item_pages);
            tv_name.setText(book.getName());
            tv_author.setText(book.getAuthor());
            tv_price.setText(book.getPrice() + "");
            tv_pages.setText(book.getPages() + "");

            return inflate;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }
    }
}
